package com.cskaoyan.service;

import com.alibaba.druid.util.StringUtils;
import com.cskaoyan.bean.bo.CreateGoodsBo;
import com.cskaoyan.bean.bo.GoodsRequestParamBo;
import com.cskaoyan.bean.bo.GoodsUpdateBo;
import com.cskaoyan.bean.bo.WxGoodsListBo;
import com.cskaoyan.bean.pojo.*;
import com.cskaoyan.bean.vo.*;
import com.cskaoyan.mapper.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.sound.midi.Soundbank;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program:
 * @description:
 * @author: Qian Jin
 * @create: 2021-07-08
 * @detail:
 **/

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    GoodsAttributeMapper goodsAttributeMapper;

    @Autowired
    GoodsProductMapper goodsProductMapper;

    @Autowired
    GoodsSpecificationMapper goodsSpecificationMapper;

    @Autowired
    GoodsCommentMapper goodsCommentMapper;

    @Autowired
    GoodsCategoryMapper goodsCategoryMapper;

    @Autowired
    GoodsBrandMapper goodsBrandMapper;

    @Autowired
    MallCouponMapper mallCouponMapper;

    @Autowired
    MallAdMapper mallAdMapper;

    @Autowired
    MallTopicMapper mallTopicMapper;

    @Autowired
    MallGrouponRulesMapper mallGrouponRulesMapper;

    @Autowired
    IssueMapper issueMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    CollectMapper collectMapper;

    @Autowired
    SearchHistoryMapper searchHistoryMapper;

    @Autowired
    FootprintMapper footprintMapper;

    @Override
    public GoodsListVo getGoodsList(GoodsRequestParamBo paramBo) {
        //分页
        PageHelper.startPage(paramBo.getPage(), paramBo.getLimit());

        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria criteria = goodsExample.createCriteria();
        //条件：删除标记为false
        criteria.andDeletedEqualTo(false);

        //订单编号
        if (paramBo.getGoodsSn() != null && paramBo.getGoodsSn().trim().length() != 0) {
            criteria.andGoodsSnEqualTo(paramBo.getGoodsSn());
        }
        //商品名称
        if (paramBo.getName() != null && paramBo.getName().trim().length() != 0) {
            criteria.andNameLike("%" + paramBo.getName() + "%");
        }
        goodsExample.setOrderByClause(paramBo.getSort() + " " + paramBo.getOrder());
        List<Goods> goods = goodsMapper.selectByExample(goodsExample);

        //总数
        PageInfo<Goods> goodsPageInfo = new PageInfo<>(goods);
        int total = (int) goodsPageInfo.getTotal();

        GoodsListVo goodsListVo = new GoodsListVo();
        goodsListVo.setItems(goods);
        goodsListVo.setTotal(total);
        return goodsListVo;

    }

    @Override
    @Transactional
    public void deleteGoods(Goods goods) {

        Integer id = goods.getId();

        try {
            int i = goodsMapper.myDeleteGoods(goods);
            int i1 = goodsMapper.myDeleteGoodsAttribute(id);
            int i2 = goodsMapper.myDeleteGoodsProduct(id);
            int i3 = goodsMapper.myDeleteGoodsSpecification(id);
        } catch (Exception e) {
            //回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }

    //新建商品
    @Override
    @Transactional
    public int createGoods(CreateGoodsBo createGoodsBo) {
        Goods goods = createGoodsBo.getGoods();
        List<GoodsProduct> products = createGoodsBo.getProducts();
        List<GoodsAttribute> attributes = createGoodsBo.getAttributes();
        List<GoodsSpecification> specifications = createGoodsBo.getSpecifications();

        //添加创建时间
        Timestamp createTime = new Timestamp(System.currentTimeMillis());

        try {
            //插入goods表
            // 设置了useGeneratedKeys="true" keyProperty="id"，后面代码可以直接取插入后goods的id
            goods.setAddTime(createTime);
            goods.setUpdateTime(createTime);
            goods.setDeleted(false);
            int i = goodsMapper.insertSelective(goods);

            //插入goodsAttribute表
            if (attributes != null) {
                for (GoodsAttribute attribute : attributes) {
                    attribute.setAddTime(createTime);
                    attribute.setUpdateTime(createTime);
                    attribute.setGoodsId(goods.getId());
                    attribute.setDeleted(false);
                    int i1 = goodsAttributeMapper.insertSelective(attribute);
                }
            }

            //插入goodsSpecification表
            if (specifications != null) {
                for (GoodsSpecification specification : specifications) {
                    specification.setAddTime(createTime);
                    specification.setUpdateTime(createTime);
                    specification.setGoodsId(goods.getId());
                    specification.setDeleted(false);
                    int i1 = goodsSpecificationMapper.insertSelective(specification);
                }
            }

            //插入goodsProduct表
            if (products != null) {
                for (GoodsProduct product : products) {
                    //！！！！注意，前端新建的product,居然给了id=1,2,3.。。,导致插入的时候，id重复。。 。手动设置null
                    product.setId(null);
                    product.setAddTime(createTime);
                    product.setUpdateTime(createTime);
                    product.setGoodsId(goods.getId());
                    product.setDeleted(false);
                    int i1 = goodsProductMapper.insertSelective(product);
                }
            }
        } catch (Exception e) {
            //事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return 500;
        }
        return 200;
    }

    @Override
    public int existGoodsSn(CreateGoodsBo createGoodsBo) {
        Goods goods = createGoodsBo.getGoods();
        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria criteria = goodsExample.createCriteria();

        //商品编号查重
        criteria.andGoodsSnEqualTo(goods.getGoodsSn());
        List<Goods> goods1 = goodsMapper.selectByExample(goodsExample);
        if (goods1.size() > 0) {
            return 401;
        }
        return 0;
    }

    @Override
    public int existGoodsName(CreateGoodsBo createGoodsBo) {
        Goods goods = createGoodsBo.getGoods();
        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria criteria = goodsExample.createCriteria();

        //商品名称查重
        criteria.andNameEqualTo(goods.getName());
        List<Goods> goods1 = goodsMapper.selectByExample(goodsExample);
        if (goods1.size() > 0) {
            return 402;
        }
        return 0;
    }

    @Override
    public GoodsCommentListVo getCommentList(GoodsRequestParamBo paramBo) {
        //分页
        PageHelper.startPage(paramBo.getPage(), paramBo.getLimit());

        GoodsCommentExample goodsCommentExample = new GoodsCommentExample();
        GoodsCommentExample.Criteria criteria = goodsCommentExample.createCriteria();
        //条件：删除标记为false
        criteria.andDeletedEqualTo(false);

        if (paramBo.getUserId() != null) {
            criteria.andUserIdEqualTo(paramBo.getUserId());
        }

        if (paramBo.getValueId() != null) {
            criteria.andValueIdEqualTo(paramBo.getValueId());
        }
        goodsCommentExample.setOrderByClause(paramBo.getSort() + " " + paramBo.getOrder());
        List<GoodsComment> goodsComments = goodsCommentMapper.selectByExample(goodsCommentExample);

        //总数
        PageInfo<GoodsComment> goodsCommentPageInfo = new PageInfo<>(goodsComments);
        int total = (int) goodsCommentPageInfo.getTotal();

        GoodsCommentListVo goodsCommentListVo = new GoodsCommentListVo();
        goodsCommentListVo.setTotal(total);
        goodsCommentListVo.setItems(goodsComments);

        return goodsCommentListVo;
    }

    @Override
    public void deleteComment(GoodsComment goodsComment) {

        int i = 0;
        try {
            i = goodsCommentMapper.myDeleteComment(goodsComment);
        } catch (Exception e) {
            //事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }

    @Override
    public CatAndBrandVo getCatAndBrand() {

        //操作goodscategory表
        GoodsCategoryExample goodsCategoryExample = new GoodsCategoryExample();
        GoodsCategoryExample.Criteria criteria = goodsCategoryExample.createCriteria();
        criteria.andDeletedEqualTo(false);

        List<GoodsCategory> goodsCategoryList = goodsCategoryMapper.selectByExample(goodsCategoryExample);

        //用来封装 list<CategoryListDTO>  -->L1
        List<CatAndBrandVo.CategoryListDTO> categoryListDTOList = new ArrayList<>();

        for (GoodsCategory goodsCategory : goodsCategoryList) {

            // 找到pid==0的category
            if (goodsCategory.getPid() == 0) {

                //封装 L1级  开始
                CatAndBrandVo.CategoryListDTO categoryListDTO = new CatAndBrandVo.CategoryListDTO();
                categoryListDTO.setValue(goodsCategory.getId());
                categoryListDTO.setLabel(goodsCategory.getName());

                //封装level L2
                //这是 list<CategoryListDTO> 中的list --> L2
                int categoryId = goodsCategory.getId();
                ArrayList<GoodsValueAndLabelVo> childrenList = new ArrayList<>();

                for (GoodsCategory category : goodsCategoryList) {
                    if (categoryId == category.getPid()) {
                        GoodsValueAndLabelVo goodsValueAndLabelVo = new GoodsValueAndLabelVo();
                        goodsValueAndLabelVo.setLabel(category.getName());
                        goodsValueAndLabelVo.setValue(category.getId());
                        //封装children List
                        childrenList.add(goodsValueAndLabelVo);
                    }
                }
                categoryListDTO.setChildren(childrenList);

                //封装 L1级  结束
                categoryListDTOList.add(categoryListDTO);
            }
        }

        // 操作goodsbrand表
        GoodsBrandExample goodsBrandExample = new GoodsBrandExample();
        GoodsBrandExample.Criteria criteria1 = goodsBrandExample.createCriteria();
        criteria1.andDeletedEqualTo(false);

        List<GoodsBrand> goodsBrands = goodsBrandMapper.selectByExample(goodsBrandExample);
        ArrayList<GoodsValueAndLabelVo> brandList = new ArrayList<>();
        for (GoodsBrand goodsBrand : goodsBrands) {
            GoodsValueAndLabelVo brand = new GoodsValueAndLabelVo();
            brand.setValue(goodsBrand.getId());
            brand.setLabel(goodsBrand.getName());
            brandList.add(brand);
        }

        CatAndBrandVo catAndBrandVo = new CatAndBrandVo();
        catAndBrandVo.setBrandList(brandList);
        catAndBrandVo.setCategoryList(categoryListDTOList);

        return catAndBrandVo;
    }

    @Override
    public GoodsDetailVo getGoodsDetail(Integer id) {
        //查商品表
        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria criteria = goodsExample.createCriteria();
        criteria.andIdEqualTo(id);
        List<Goods> goods = goodsMapper.selectByExample(goodsExample);

        //查货品表
        GoodsProductExample goodsProductExample = new GoodsProductExample();
        GoodsProductExample.Criteria criteria1 = goodsProductExample.createCriteria();
        criteria1.andGoodsIdEqualTo(id).andDeletedEqualTo(false);
        List<GoodsProduct> goodsProducts = goodsProductMapper.selectByExample(goodsProductExample);

        //查商品规格表
        GoodsSpecificationExample goodsSpecificationExample = new GoodsSpecificationExample();
        GoodsSpecificationExample.Criteria criteria2 = goodsSpecificationExample.createCriteria();
        criteria2.andGoodsIdEqualTo(id).andDeletedEqualTo(false);
        List<GoodsSpecification> goodsSpecifications = goodsSpecificationMapper.selectByExample(goodsSpecificationExample);

        //查商品参数表
        GoodsAttributeExample goodsAttributeExample = new GoodsAttributeExample();
        GoodsAttributeExample.Criteria criteria3 = goodsAttributeExample.createCriteria();
        criteria3.andGoodsIdEqualTo(id).andDeletedEqualTo(false);
        List<GoodsAttribute> goodsAttributes = goodsAttributeMapper.selectByExample(goodsAttributeExample);

        //查分类表  拿goods表中CategoryId去表里取pId
        GoodsCategoryExample goodsCategoryExample = new GoodsCategoryExample();
        GoodsCategoryExample.Criteria criteria4 = goodsCategoryExample.createCriteria();
        Goods goods1 = goods.get(0);
        int goodsCategoryId = goods1.getCategoryId();
        criteria4.andIdEqualTo(goodsCategoryId).andDeletedEqualTo(false);
        List<GoodsCategory> goodsCategories = goodsCategoryMapper.selectByExample(goodsCategoryExample);

        //封装
        GoodsDetailVo goodsDetailVo = new GoodsDetailVo();
        //封装categoryId[]
        //[0]: 存分类的父类   [1]: 分类的子类
        if (goodsCategories.size() != 0) {
            GoodsCategory goodsCategory = goodsCategories.get(0);
            Integer pid = goodsCategory.getPid();

            Integer[] integer = new Integer[2];
            integer[0] = pid;
            integer[1] = goodsCategoryId;
            goodsDetailVo.setCategoryIds(integer);
        }

        //封装goods GoodsAttribute GoodsSpecification
        goodsDetailVo.setGoods(goods1);
        goodsDetailVo.setAttributes(goodsAttributes);
        goodsDetailVo.setProducts(goodsProducts);
        goodsDetailVo.setSpecifications(goodsSpecifications);
        return goodsDetailVo;
    }

    //更新商品
    @Override
    @Transactional
    public int updateGoods(GoodsUpdateBo goodsUpdateBo) {
        Goods goods = goodsUpdateBo.getGoods();
        List<GoodsProduct> products = goodsUpdateBo.getProducts();
        List<GoodsAttribute> attributes = goodsUpdateBo.getAttributes();
        List<GoodsSpecification> specifications = goodsUpdateBo.getSpecifications();

        //添加更新时间
        Timestamp updateTime = new Timestamp(System.currentTimeMillis());

        //商品ID
        int goodsId = goods.getId();

        //商品信息处理 1，商品编号和名称查重；2 updateTime 更新 ； 3 其他直接更新
        if (existGoodsNameOfUpdate(goods)) {
            return 401;
        }
        if (existGoodsSnOfUpdate(goods)) {
            return 402;
        }

        //数据库更新
        /*
         *
         * 新添加的规格，只传进要存的内容，没有id，这个进行插入操作; 在原规格上修改的规格，传进id+内容，这个进行修改操作
         *
         * 由于规格之类的数据删除之后，前端不会传信息回来，所以在进行完上一步操作之后，要把该商品下除了传进来的id以及新插入的id之外的规格删除
         *
         * */
        //更新逻辑：1，
        try {
            //商品表更新
            GoodsExample goodsExample = new GoodsExample();
            GoodsExample.Criteria goodsExampleCriteria = goodsExample.createCriteria();
            goods.setUpdateTime(updateTime);
            goodsExampleCriteria.andIdEqualTo(goodsId);
            int i = goodsMapper.updateByExampleSelective(goods, goodsExample);

            //商品参数表更新
            if (attributes != null) {

                ArrayList<Integer> arrayList = new ArrayList<>();
                for (GoodsAttribute attribute : attributes) {
                    //更新时间
                    attribute.setUpdateTime(updateTime);
                    //新增参数 直接插入
                    if (attribute.getId() == null) {
                        attribute.setAddTime(updateTime);
                        attribute.setGoodsId(goodsId);
                        int i1 = goodsAttributeMapper.insertSelective(attribute);
                        //非常关键   useGeneratedKeys="true" keyProperty="id"  取到插入后的id，加入list
                        arrayList.add(attribute.getId());
                    } else {//修改参数 在原始数据上修改

                        GoodsAttributeExample goodsAttributeExample = new GoodsAttributeExample();
                        GoodsAttributeExample.Criteria attributeExampleCriteria = goodsAttributeExample.createCriteria();
                        attributeExampleCriteria.andIdEqualTo(attribute.getId());
                        int i1 = goodsAttributeMapper.updateByExampleSelective(attribute, goodsAttributeExample);
                        arrayList.add(attribute.getId());
                    }
                }

                //把该商品下除了传进来的id以及新插入的id之外的规格删除
                if (arrayList.size() > 0) {
                    //List＜Integer＞ 转换成以逗号分割的String字符串
                    String strList = arrayList.stream().map(String::valueOf).collect(Collectors.joining(","));
                    int d = goodsAttributeMapper.myDeleteGoodsAttributeList(goodsId, strList);
                }
            }

            //货品表更新
            if (products != null) {
                ArrayList<Integer> arrayList = new ArrayList<>();
                for (GoodsProduct product : products) {
                    product.setUpdateTime(updateTime);
                    if (product.getId() == null) {
                        product.setAddTime(updateTime);
                        product.setGoodsId(goodsId);
                        int i1 = goodsProductMapper.insertSelective(product);
                        //非常关键  useGeneratedKeys="true" keyProperty="id"  取到插入后的id，加入list
                        arrayList.add(product.getId());
                    } else {
                        GoodsProductExample goodsProductExample = new GoodsProductExample();
                        GoodsProductExample.Criteria productExampleCriteria = goodsProductExample.createCriteria();

                        productExampleCriteria.andIdEqualTo(product.getId());
                        int i1 = goodsProductMapper.updateByExampleSelective(product, goodsProductExample);
                        arrayList.add(product.getId());
                    }
                }
                //把该商品下除了传进来的id以及新插入的id之外的规格删除
                if (arrayList.size() > 0) {
                    //List＜Integer＞ 转换成以逗号分割的String字符串
                    String strList = arrayList.stream().map(String::valueOf).collect(Collectors.joining(","));
                    int d = goodsProductMapper.myDeleteGoodsProductList(goodsId, strList);
                }
            }

            //规格表更新
            if (specifications != null) {
                ArrayList<Integer> arrayList = new ArrayList<>();
                for (GoodsSpecification specification : specifications) {
                    specification.setUpdateTime(updateTime);
                    if (specification.getId() == null) {
                        specification.setAddTime(updateTime);
                        specification.setGoodsId(goodsId);
                        int i1 = goodsSpecificationMapper.insertSelective(specification);
                        //非常关键 取到插入后的id，加入list
                        arrayList.add(specification.getId());
                    } else {
                        GoodsSpecificationExample goodsSpecificationExample = new GoodsSpecificationExample();
                        GoodsSpecificationExample.Criteria specificationExampleCriteria = goodsSpecificationExample.createCriteria();

                        specificationExampleCriteria.andIdEqualTo(specification.getId());
                        int i1 = goodsSpecificationMapper.updateByExampleSelective(specification, goodsSpecificationExample);
                        arrayList.add(specification.getId());
                    }
                }
                //把该商品下除了传进来的id以及新插入的id之外的规格删除
                if (arrayList.size() > 0) {
                    //List＜Integer＞ 转换成以逗号分割的String字符串
                    String strList = arrayList.stream().map(String::valueOf).collect(Collectors.joining(","));
                    int d = goodsSpecificationMapper.myDeleteGoodsSpecificationList(goodsId, strList);
                }
            }

        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return 500;
        }
        return 200;
    }

    @Override
    public WxGoodsCountVo getWxGoodsCount() {
        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria criteria = goodsExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        long countLong = goodsMapper.countByExample(goodsExample);
        int count = (int) countLong;
        WxGoodsCountVo wxGoodsCountVo = new WxGoodsCountVo();
        wxGoodsCountVo.setGoodsCount(count);
        return wxGoodsCountVo;
    }

    @Override
    public WxGoodsListVo getWxGoodsList(WxGoodsListBo wxGoodsListBo) {
        WxGoodsListVo wxGoodsListVo = new WxGoodsListVo();
        //分页
        PageHelper.startPage(wxGoodsListBo.getPage(), wxGoodsListBo.getSize());

        if (wxGoodsListBo.getCategoryId() != null && wxGoodsListBo.getKeyword() == null) {
            //goods表查出categoryId下的商品
            GoodsExample goodsExample = new GoodsExample();
            GoodsExample.Criteria goodsExampleCriteriacriteria = goodsExample.createCriteria();
            goodsExampleCriteriacriteria.andCategoryIdEqualTo(wxGoodsListBo.getCategoryId()).andDeletedEqualTo(false);
            goodsExample.setOrderByClause("sort_order" + " " + "desc");
            List<Goods> goods = goodsMapper.selectByExample(goodsExample);

            if (!goods.isEmpty()) {
                //把该L2类别的同类查找出来
                Integer id = wxGoodsListBo.getCategoryId();
                GoodsCategory goodsCategory = goodsCategoryMapper.selectByPrimaryKey(id);
                Integer pid = goodsCategory.getPid();
                GoodsCategoryExample catelogGoodsCategoryExample = new GoodsCategoryExample();
                GoodsCategoryExample.Criteria goodsCategoryExampleCriteria = catelogGoodsCategoryExample.createCriteria();
                goodsCategoryExampleCriteria.andDeletedEqualTo(false).andPidEqualTo(pid);
                catelogGoodsCategoryExample.setOrderByClause("sort_order" + " " + "desc");
                List<GoodsCategory> goodsCategoryL2List = goodsCategoryMapper.selectByExample(catelogGoodsCategoryExample);
                wxGoodsListVo.setFilterCategoryList(goodsCategoryL2List);

            }

            //总数
            PageInfo<Goods> goodsPageInfo = new PageInfo<>(goods);
            int total = (int) goodsPageInfo.getTotal();

            wxGoodsListVo.setCount(total);
            wxGoodsListVo.setGoodsList(goods);

            return wxGoodsListVo;

        } else if (wxGoodsListBo.getBrandId() != null && wxGoodsListBo.getKeyword() == null) {
            //查找品牌下所有的商品
            Integer brandId = wxGoodsListBo.getBrandId();
            GoodsExample goodsExample = new GoodsExample();
            GoodsExample.Criteria goodsExampleCriteriacriteria = goodsExample.createCriteria();
            goodsExampleCriteriacriteria.andDeletedEqualTo(false).andBrandIdEqualTo(brandId);
            goodsExample.setOrderByClause("sort_order" + " " + "desc");
            List<Goods> goods = goodsMapper.selectByExample(goodsExample);
            wxGoodsListVo.setGoodsList(goods);

            ArrayList<Integer> categoryIDlist = new ArrayList<>();
            if (!goods.isEmpty()) {
                for (Goods good : goods) {
                    categoryIDlist.add(good.getCategoryId());
                }
                //查找商品对应的分类
                GoodsCategoryExample catelogGoodsCategoryExample = new GoodsCategoryExample();
                GoodsCategoryExample.Criteria goodsCategoryExampleCriteria = catelogGoodsCategoryExample.createCriteria();
                goodsCategoryExampleCriteria.andIdIn(categoryIDlist).andDeletedEqualTo(false);
                catelogGoodsCategoryExample.setOrderByClause("sort_order" + " " + "desc");
                List<GoodsCategory> goodsCategories = goodsCategoryMapper.selectByExample(catelogGoodsCategoryExample);
                wxGoodsListVo.setFilterCategoryList(goodsCategories);
            }

            //总数
            PageInfo<Goods> goodsPageInfo = new PageInfo<>(goods);
            int total = (int) goodsPageInfo.getTotal();
            wxGoodsListVo.setCount(total);

            return wxGoodsListVo;

        } else {
            //搜索关键词
            String keyword = wxGoodsListBo.getKeyword();
            //搜索记录-->存表
            //获取用户id
            Subject subject = SecurityUtils.getSubject();
            User user = (User) subject.getPrincipals().getPrimaryPrincipal();
            Integer userId = user.getId();
            //添加创建时间
            Timestamp createTime = new Timestamp(System.currentTimeMillis());

            //先查数据库该用户是否搜索过这个词。如果是，只更新updateTime；如果否，直接插入数据；
            SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
            SearchHistoryExample.Criteria searchHistoryExampleCriteria = searchHistoryExample.createCriteria();
            searchHistoryExampleCriteria.andKeywordEqualTo(keyword).andUserIdEqualTo(userId)
                    .andDeletedEqualTo(false);
            long count = searchHistoryMapper.countByExample(searchHistoryExample);
            //如果空，则没有这个关键词，直接插入
            if (count == 0) {
                SearchHistory searchHistory = new SearchHistory();
                searchHistory.setUserId(userId);
                searchHistory.setAddTime(createTime);
                searchHistory.setUpdateTime(createTime);
                searchHistory.setFrom("wx");
                searchHistory.setDeleted(false);
                searchHistory.setKeyword(keyword);
                int i = searchHistoryMapper.insertSelective(searchHistory);
            } else {//已有记录，只更新时间
                SearchHistory searchHistory = new SearchHistory();
                searchHistory.setUpdateTime(createTime);
                SearchHistoryExample searchHistoryExampleHadExist = new SearchHistoryExample();
                SearchHistoryExample.Criteria criteria = searchHistoryExampleHadExist.createCriteria();
                criteria.andKeywordEqualTo(keyword).andUserIdEqualTo(userId)
                        .andDeletedEqualTo(false);
                int i = searchHistoryMapper.updateByExampleSelective(searchHistory, searchHistoryExampleHadExist);
            }

            //模糊搜索商品
            GoodsExample goodsExample = new GoodsExample();
            GoodsExample.Criteria goodsExampleCriteriacriteria = goodsExample.createCriteria();
            goodsExampleCriteriacriteria.andNameLike("%" + keyword + "%").andDeletedEqualTo(false);
            goodsExample.setOrderByClause(wxGoodsListBo.getSort() + " " + wxGoodsListBo.getOrder());
            List<Goods> goods = goodsMapper.selectByExample(goodsExample);
            wxGoodsListVo.setGoodsList(goods);

            ArrayList<Integer> categoryIDlist = new ArrayList<>();
            if (!goods.isEmpty()) {
                for (Goods good : goods) {
                    categoryIDlist.add(good.getCategoryId());
                }
                //查找商品对应的分类
                GoodsCategoryExample catelogGoodsCategoryExample = new GoodsCategoryExample();
                GoodsCategoryExample.Criteria goodsCategoryExampleCriteria = catelogGoodsCategoryExample.createCriteria();
                goodsCategoryExampleCriteria.andIdIn(categoryIDlist).andDeletedEqualTo(false);
                catelogGoodsCategoryExample.setOrderByClause("sort_order" + " " + "desc");
                List<GoodsCategory> goodsCategories = goodsCategoryMapper.selectByExample(catelogGoodsCategoryExample);
                wxGoodsListVo.setFilterCategoryList(goodsCategories);
            }

            //总数
            PageInfo<Goods> goodsPageInfo = new PageInfo<>(goods);
            int total = (int) goodsPageInfo.getTotal();
            wxGoodsListVo.setCount(total);

            return wxGoodsListVo;
        }
    }

    @Override
    public WxHomeIndexVo getWxHomeIndex() {

        WxHomeIndexVo wxHomeIndexVo = new WxHomeIndexVo();

        //新品 展示6个
        GoodsExample isNewGoodsExample = new GoodsExample();
        GoodsExample.Criteria isNewGoodsExampleCriteria = isNewGoodsExample.createCriteria();
        isNewGoodsExampleCriteria.andIsNewEqualTo(true).andDeletedEqualTo(false).andIsOnSaleEqualTo(true);
        isNewGoodsExample.setOrderByClause("sort_order" + " " + "desc" + " " + "limit 6");
        List<Goods> isNewGoods = goodsMapper.selectByExample(isNewGoodsExample);
        wxHomeIndexVo.setNewGoodsList(isNewGoods);

        //热卖 展示6个  //todo  1 限制功能， 2 排序条件
        GoodsExample isHotGoodsExample = new GoodsExample();
        GoodsExample.Criteria isHotGoodsExampleCriteria = isHotGoodsExample.createCriteria();
        isHotGoodsExampleCriteria.andIsHotEqualTo(true).andDeletedEqualTo(false).andIsOnSaleEqualTo(true);
        isHotGoodsExample.setOrderByClause("sort_order" + " " + "desc" + " " + "limit 6");
        List<Goods> isHotGoods = goodsMapper.selectByExample(isHotGoodsExample);
        wxHomeIndexVo.setHotGoodsList(isHotGoods);

        //优惠券 显示3个
        MallCouponExample mallCouponExample = new MallCouponExample();
        MallCouponExample.Criteria couponExampleCriteria = mallCouponExample.createCriteria();
        couponExampleCriteria.andDeletedEqualTo(false);
        mallCouponExample.setOrderByClause("update_time" + " " + "desc" + " " + "limit 3");
        List<MallCoupon> mallCoupons = mallCouponMapper.selectByExample(mallCouponExample);
        wxHomeIndexVo.setCouponList(mallCoupons);

        //L1级分类
        GoodsCategoryExample goodsCategoryExample = new GoodsCategoryExample();
        GoodsCategoryExample.Criteria exampleCriteria = goodsCategoryExample.createCriteria();
        exampleCriteria.andLevelEqualTo("L1").andDeletedEqualTo(false);
        goodsCategoryExample.setOrderByClause("sort_order" + " " + "desc");
        List<GoodsCategory> goodsCategoriesL1 = goodsCategoryMapper.selectByExample(goodsCategoryExample);
        wxHomeIndexVo.setChannel(goodsCategoriesL1);

        //广告
        MallAdExample mallAdExample = new MallAdExample();
        MallAdExample.Criteria adExampleCriteria = mallAdExample.createCriteria();
        adExampleCriteria.andDeletedEqualTo(false);
        mallAdExample.setOrderByClause("update_time" + " " + "desc");
        List<MallAd> mallAds = mallAdMapper.selectByExample(mallAdExample);
        wxHomeIndexVo.setBanner(mallAds);

        //品牌
        GoodsBrandExample goodsBrandExample = new GoodsBrandExample();
        GoodsBrandExample.Criteria brandExampleCriteria = goodsBrandExample.createCriteria();
        brandExampleCriteria.andDeletedEqualTo(false);
        goodsBrandExample.setOrderByClause("sort_order" + " " + "desc" + " " + "limit 8");
        List<GoodsBrand> goodsBrands = goodsBrandMapper.selectByExample(goodsBrandExample);
        wxHomeIndexVo.setBrandList(goodsBrands);

        //主题
        MallTopicExample mallTopicExample = new MallTopicExample();
        MallTopicExample.Criteria topicExampleCriteria = mallTopicExample.createCriteria();
        topicExampleCriteria.andDeletedEqualTo(false);
        mallTopicExample.setOrderByClause("sort_order" + " " + "desc");
        List<MallTopic> mallTopics = mallTopicMapper.selectByExample(mallTopicExample);
        wxHomeIndexVo.setTopicList(mallTopics);

        //团购
        MallGrouponRulesExample mallGrouponRulesExample = new MallGrouponRulesExample();
        MallGrouponRulesExample.Criteria grouponRulesExampleCriteria = mallGrouponRulesExample.createCriteria();
        grouponRulesExampleCriteria.andDeletedEqualTo(false);
        List<MallGrouponRules> mallGrouponRules = mallGrouponRulesMapper.selectByExample(mallGrouponRulesExample);
        //整合封装
        ArrayList<WxHomeIndexVo.GrouponListDTO> grouponListDTOS = new ArrayList<>();
        for (MallGrouponRules grouponRule : mallGrouponRules) {
            WxHomeIndexVo.GrouponListDTO grouponListDTO = new WxHomeIndexVo.GrouponListDTO();
            Integer goodsId = grouponRule.getGoodsId();
            GoodsExample goodsExample = new GoodsExample();
            Goods goods = goodsMapper.selectByPrimaryKey(goodsId);

            //团购价格
            BigDecimal discount = grouponRule.getDiscount();
            BigDecimal retailPrice = goods.getRetailPrice();
            BigDecimal grouponPrice = retailPrice.subtract(discount);
            //内部封装
            grouponListDTO.setGroupon_price(grouponPrice);
            grouponListDTO.setGroupon_member(grouponRule.getDiscountMember());
            grouponListDTO.setGoods(goods);
            grouponListDTOS.add(grouponListDTO);

            //限制展示数量 3
            if (grouponListDTOS.size() >= 3) {
                break;
            }
        }
        wxHomeIndexVo.setGrouponList(grouponListDTOS);

        //L1级分类下的商品
        ArrayList<WxHomeIndexVo.FloorGoodsListDTO> floorGoodsListDTOS = new ArrayList<>();
        for (GoodsCategory goodsCategory : goodsCategoriesL1) {
            //把每一个L1级下面的L2目录全查出
            Integer categoryId = goodsCategory.getId();
            GoodsCategoryExample floorGoodsCategoryExample = new GoodsCategoryExample();
            GoodsCategoryExample.Criteria goodsCategoryExampleCriteria = floorGoodsCategoryExample.createCriteria();
            goodsCategoryExampleCriteria.andDeletedEqualTo(false).andPidEqualTo(categoryId);
            floorGoodsCategoryExample.setOrderByClause("sort_order" + " " + "desc");
            List<GoodsCategory> goodsCategoryL2List = goodsCategoryMapper.selectByExample(floorGoodsCategoryExample);
            //再把L2级下面每个商品查出
            ArrayList<Goods> goodsOfL1 = new ArrayList<>();
            WxHomeIndexVo.FloorGoodsListDTO floorGoodsListDTO = new WxHomeIndexVo.FloorGoodsListDTO();
            L1:
            for (GoodsCategory categoryL2 : goodsCategoryL2List) {
                Integer l2Id = categoryL2.getId();
                GoodsExample goodsExample = new GoodsExample();
                GoodsExample.Criteria goodsExampleCriteria = goodsExample.createCriteria();
                goodsExampleCriteria.andCategoryIdEqualTo(l2Id).andDeletedEqualTo(false).andIsOnSaleEqualTo(true);
                goodsExample.setOrderByClause("sort_order" + " " + "desc");
                List<Goods> goods = goodsMapper.selectByExample(goodsExample);

                //  限制展示数量 ：4
                for (Goods good : goods) {
                    goodsOfL1.add(good);
                    if (goodsOfL1.size() >= 4) {
                        break L1;
                    }
                }
            }

            floorGoodsListDTO.setId(goodsCategory.getId());
            floorGoodsListDTO.setName(goodsCategory.getName());
            floorGoodsListDTO.setGoodsList(goodsOfL1);
            floorGoodsListDTOS.add(floorGoodsListDTO);
        }
        wxHomeIndexVo.setFloorGoodsList(floorGoodsListDTOS);

        return wxHomeIndexVo;
    }

    @Override
    public WxCatalogIndexVo getCatalogIndex() {

        WxCatalogIndexVo wxCatalogIndexVo = new WxCatalogIndexVo();

        //查出L1级分类
        GoodsCategoryExample goodsCategoryExample = new GoodsCategoryExample();
        GoodsCategoryExample.Criteria exampleCriteria = goodsCategoryExample.createCriteria();
        exampleCriteria.andLevelEqualTo("L1").andDeletedEqualTo(false);
        goodsCategoryExample.setOrderByClause("sort_order" + " " + "desc");
        List<GoodsCategory> goodsCategoriesL1 = goodsCategoryMapper.selectByExample(goodsCategoryExample);
        wxCatalogIndexVo.setCategoryList(goodsCategoriesL1);

        if (goodsCategoriesL1.size() > 0) {
            GoodsCategory goodsCategory = goodsCategoriesL1.get(0);
            //取第一个L1分类
            wxCatalogIndexVo.setCurrentCategory(goodsCategory);

            //把当前L1级下面的L2目录全查出
            Integer id = goodsCategory.getId();
            GoodsCategoryExample catelogGoodsCategoryExample = new GoodsCategoryExample();
            GoodsCategoryExample.Criteria goodsCategoryExampleCriteria = catelogGoodsCategoryExample.createCriteria();
            goodsCategoryExampleCriteria.andDeletedEqualTo(false).andPidEqualTo(id);
            catelogGoodsCategoryExample.setOrderByClause("sort_order" + " " + "desc");
            List<GoodsCategory> goodsCategoryL2List = goodsCategoryMapper.selectByExample(catelogGoodsCategoryExample);

            wxCatalogIndexVo.setCurrentSubCategory(goodsCategoryL2List);
        }
        return wxCatalogIndexVo;
    }

    @Override
    public WxCatalogCurrentVo getCatalogCurrent(Integer id) {

        WxCatalogCurrentVo wxCatalogCurrentVo = new WxCatalogCurrentVo();

        //查出id的分类详情
        GoodsCategory goodsCategory = goodsCategoryMapper.selectByPrimaryKey(id);
        wxCatalogCurrentVo.setCurrentCategory(goodsCategory);

        //查出id下的L2级分类
        GoodsCategoryExample goodsCategoryExample = new GoodsCategoryExample();
        GoodsCategoryExample.Criteria exampleCriteria = goodsCategoryExample.createCriteria();
        exampleCriteria.andDeletedEqualTo(false).andPidEqualTo(id);
        goodsCategoryExample.setOrderByClause("sort_order" + " " + "desc");
        List<GoodsCategory> goodsCategoriesL2 = goodsCategoryMapper.selectByExample(goodsCategoryExample);
        wxCatalogCurrentVo.setCurrentSubCategory(goodsCategoriesL2);

        return wxCatalogCurrentVo;
    }

    @Override
    public WxGoodsCategoryVo getGoodsCategory(Integer id) {

        WxGoodsCategoryVo wxGoodsCategoryVo = new WxGoodsCategoryVo();

        //当前的L2分类信息
        GoodsCategory goodsCategory = goodsCategoryMapper.selectByPrimaryKey(id);
        wxGoodsCategoryVo.setCurrentCategory(goodsCategory);

        //当前L2分类的父类
        GoodsCategory goodsCategoryParent = goodsCategoryMapper.selectByPrimaryKey(goodsCategory.getPid());
        wxGoodsCategoryVo.setParentCategory(goodsCategoryParent);

        //当前L2分类的同类
        GoodsCategoryExample goodsCategoryExample = new GoodsCategoryExample();
        GoodsCategoryExample.Criteria exampleCriteria = goodsCategoryExample.createCriteria();
        exampleCriteria.andDeletedEqualTo(false).andPidEqualTo(goodsCategory.getPid()).andIdNotEqualTo(id);
        goodsCategoryExample.setOrderByClause("sort_order" + " " + "desc");
        List<GoodsCategory> goodsCategories = goodsCategoryMapper.selectByExample(goodsCategoryExample);
        wxGoodsCategoryVo.setBrotherCategory(goodsCategories);

        return wxGoodsCategoryVo;
    }

    @Override
    public WxGoodsBrandDetailVo getBrandDetail(Integer id) {
        WxGoodsBrandDetailVo wxGoodsBrandDetailVo = new WxGoodsBrandDetailVo();
        GoodsBrand goodsBrand = goodsBrandMapper.selectByPrimaryKey(id);
        wxGoodsBrandDetailVo.setBrand(goodsBrand);
        return wxGoodsBrandDetailVo;
    }

    @Override
    public WxGoodsBrandListVo getBrandList(Integer page, Integer size) {

        WxGoodsBrandListVo wxGoodsBrandListVo = new WxGoodsBrandListVo();

        //分页
        PageHelper.startPage(page, size);

        //品牌列表
        GoodsBrandExample goodsBrandExample = new GoodsBrandExample();
        GoodsBrandExample.Criteria criteria = goodsBrandExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        goodsBrandExample.setOrderByClause("sort_order" + " " + "desc");
        List<GoodsBrand> goodsBrandList = goodsBrandMapper.selectByExample(goodsBrandExample);
        wxGoodsBrandListVo.setBrandList(goodsBrandList);

        //总页数
        PageInfo<GoodsBrand> goodsBrandPageInfo = new PageInfo<>(goodsBrandList);
        int pageSize = goodsBrandPageInfo.getPageSize();
        wxGoodsBrandListVo.setTotalPages(pageSize);

        return wxGoodsBrandListVo;
    }

    @Override
    public WxGoodsDetailVo getWxGoodsDetail(Integer id) {
        //商品详情
        WxGoodsDetailVo wxGoodsDetailVo = new WxGoodsDetailVo();

        GoodsExample goodsExample = new GoodsExample();
        goodsExample.createCriteria().andDeletedEqualTo(false).andIdEqualTo(id);
        List<Goods> goodsList = goodsMapper.selectByExample(goodsExample);

        //当从历史足迹中进来的时候，商品有可能被删除了，所以进行判断
        if (!goodsList.isEmpty()) {
            //商品还在，正常来说List里只有一个goods对象
            Goods goods = goodsList.get(0);
            wxGoodsDetailVo.setInfo(goods);

            //分享的图片
            String[] gallery = goods.getGallery();
            if (gallery.length > 0) {
                String s = gallery[0];
                wxGoodsDetailVo.setShareImage(s);
            }

            //查货品表
            GoodsProductExample goodsProductExample = new GoodsProductExample();
            GoodsProductExample.Criteria criteria1 = goodsProductExample.createCriteria();
            criteria1.andGoodsIdEqualTo(id).andDeletedEqualTo(false);
            List<GoodsProduct> goodsProducts = goodsProductMapper.selectByExample(goodsProductExample);
            wxGoodsDetailVo.setProductList(goodsProducts);

            //查商品参数表
            GoodsAttributeExample goodsAttributeExample = new GoodsAttributeExample();
            GoodsAttributeExample.Criteria criteria3 = goodsAttributeExample.createCriteria();
            criteria3.andGoodsIdEqualTo(id).andDeletedEqualTo(false);
            List<GoodsAttribute> goodsAttributes = goodsAttributeMapper.selectByExample(goodsAttributeExample);
            wxGoodsDetailVo.setAttribute(goodsAttributes);

            //查问题表
            IssueExample issueExample = new IssueExample();
            IssueExample.Criteria issueExampleCriteria = issueExample.createCriteria();
            issueExampleCriteria.andDeletedEqualTo(0);
            List<Issue> issues = issueMapper.selectByExample(issueExample);
            wxGoodsDetailVo.setIssue(issues);

            //商品品牌
            GoodsBrand goodsBrand = goodsBrandMapper.selectByPrimaryKey(goods.getBrandId());
            wxGoodsDetailVo.setBrand(goodsBrand);

            //商品评论
            GoodsCommentExample goodsCommentExample = new GoodsCommentExample();
            GoodsCommentExample.Criteria commentExampleCriteria = goodsCommentExample.createCriteria();
            commentExampleCriteria.andValueIdEqualTo(id).andDeletedEqualTo(false);
            List<GoodsComment> goodsComments = goodsCommentMapper.selectByExample(goodsCommentExample);
            //封装CommentDTO
            WxGoodsDetailVo.CommentDTO commentDTO = new WxGoodsDetailVo.CommentDTO();
            ArrayList<WxGoodsDetailVo.CommentDTO.DataDTO> dataDTOS = new ArrayList<>();
            //封装count
            commentDTO.setCount(goodsComments.size());//评论数量
            //封装data
            for (GoodsComment goodsComment : goodsComments) {
                User user = userMapper.selectByPrimaryKey(goodsComment.getUserId());
                WxGoodsDetailVo.CommentDTO.DataDTO dataDTO = new WxGoodsDetailVo.CommentDTO.DataDTO();
                dataDTO.setId(goodsComment.getId());
                dataDTO.setPicList(goodsComment.getPicUrls());
                dataDTO.setAddTime(goodsComment.getAddTime());
                dataDTO.setContent(goodsComment.getContent());
                dataDTO.setAvatar(user.getAvatar());
                dataDTO.setNickname(user.getNickname());
                dataDTOS.add(dataDTO);
            }
            commentDTO.setData(dataDTOS);
            wxGoodsDetailVo.setComment(commentDTO);

            //团购
            MallGrouponRulesExample mallGrouponRulesExample = new MallGrouponRulesExample();
            MallGrouponRulesExample.Criteria grouponRulesExampleCriteria = mallGrouponRulesExample.createCriteria();
            grouponRulesExampleCriteria.andDeletedEqualTo(false);
            List<MallGrouponRules> mallGrouponRules = mallGrouponRulesMapper.selectByExample(mallGrouponRulesExample);
            wxGoodsDetailVo.setGroupon(mallGrouponRules);

            //查商品规格表
            GoodsSpecificationExample goodsSpecificationExample = new GoodsSpecificationExample();
            GoodsSpecificationExample.Criteria criteria2 = goodsSpecificationExample.createCriteria();
            criteria2.andGoodsIdEqualTo(id).andDeletedEqualTo(false);
            List<GoodsSpecification> goodsSpecifications = goodsSpecificationMapper.selectByExample(goodsSpecificationExample);
            //这里要注意了
            //同一个商品下面可以有多个Specification，同一个Specification也可以对应多个value
            //先用hashSet筛选出Specification，再封装value
            HashSet<String> hashSet = new HashSet<>();
            for (GoodsSpecification goodsSpecification : goodsSpecifications) {
                hashSet.add(goodsSpecification.getSpecification());
            }
            //封装specificationListDTOS
            ArrayList<WxGoodsDetailVo.SpecificationListDTO> specificationListDTOS = new ArrayList<>();
            Iterator<String> iterator = hashSet.iterator();
            //取出
            while (iterator.hasNext()) {
                //取出一个specification
                String specification = iterator.next();
                //封装内部SpecificationListDTO -> name
                WxGoodsDetailVo.SpecificationListDTO specificationListDTO = new WxGoodsDetailVo.SpecificationListDTO();
                specificationListDTO.setName(specification);
                //用集合装Specification对应的Value
                ArrayList<GoodsSpecification> specificationArrayList = new ArrayList<>();
                //遍历筛选
                for (GoodsSpecification goodsSpecification : goodsSpecifications) {
                    //条件：该条规格的specification与此次循环中的specification相同
                    if (specification.equals(goodsSpecification.getSpecification())) {
                        //加入集合
                        specificationArrayList.add(goodsSpecification);
                    }
                }
                //封装内部SpecificationListDTO -> specificationList
                specificationListDTO.setValueList(specificationArrayList);
                //封装完成后 添加到specificationListDTOS
                specificationListDTOS.add(specificationListDTO);
            }
            wxGoodsDetailVo.setSpecificationList(specificationListDTOS);

            //用户是否收藏
            //取出当前登陆的用户的id
            Subject subject = SecurityUtils.getSubject();
            User user = (User) subject.getPrincipals().getPrimaryPrincipal();
            Integer userId = user.getId();

            CollectExample collectExample = new CollectExample();
            CollectExample.Criteria collectExampleCriteria = collectExample.createCriteria();
            collectExampleCriteria.andTypeEqualTo((byte) 0).andUserIdEqualTo(userId).andValueIdEqualTo(goods.getId()).andDeletedEqualTo(false);
            long count = collectMapper.countByExample(collectExample);
            if (count == 0) {
                wxGoodsDetailVo.setUserHasCollect(0);
            } else {
                wxGoodsDetailVo.setUserHasCollect(1);
            }

            //历史足迹,先去数据库搜索是否浏览过这个商品，如果有，只更新时间；如果没有，则插入数据
            Timestamp createTime = new Timestamp(System.currentTimeMillis());
            FootprintExample footprintExample = new FootprintExample();
            FootprintExample.Criteria footprintExampleCriteria = footprintExample.createCriteria();
            footprintExampleCriteria.andDeletedEqualTo(false).andUserIdEqualTo(userId).andGoodsIdEqualTo(id);
            long count1 = footprintMapper.countByExample(footprintExample);
            if (count1 == 0) {
                Footprint footprint = new Footprint();
                footprint.setAddTime(createTime);
                footprint.setUpdateTime(createTime);
                footprint.setUserId(userId);
                footprint.setGoodsId(id);
                footprint.setDeleted(false);
                int i = footprintMapper.insertSelective(footprint);
            } else {//已有记录,只更新时间
                Footprint footprint = new Footprint();
                footprint.setUpdateTime(createTime);
                FootprintExample footprintExampleHadExist = new FootprintExample();
                FootprintExample.Criteria footprintExampleHadExistCriteria = footprintExampleHadExist.createCriteria();
                footprintExampleHadExistCriteria.andDeletedEqualTo(false).andUserIdEqualTo(userId).andGoodsIdEqualTo(id);
                int i = footprintMapper.updateByExampleSelective(footprint, footprintExampleHadExist);
            }

            return wxGoodsDetailVo;
        } else {//商品不存在，返回一个空对象
            return wxGoodsDetailVo;
        }
    }

    @Override
    public WxGoodsRelatedVo getGoodsRelated(Integer id) {
        Goods goods = goodsMapper.selectByPrimaryKey(id);
        Integer categoryId = goods.getCategoryId();

        GoodsExample isNewGoodsExample = new GoodsExample();
        GoodsExample.Criteria isNewGoodsExampleCriteria = isNewGoodsExample.createCriteria();
        isNewGoodsExampleCriteria.andCategoryIdEqualTo(categoryId).andDeletedEqualTo(false).andIsOnSaleEqualTo(true);
        isNewGoodsExample.setOrderByClause("sort_order" + " " + "desc" + " " + "limit 4");
        List<Goods> goodsList = goodsMapper.selectByExample(isNewGoodsExample);
        //list 转成 数组
        Goods[] goodsArr = new Goods[goodsList.size()];
        goodsList.toArray(goodsArr);

        WxGoodsRelatedVo wxGoodsRelatedVo = new WxGoodsRelatedVo();
        wxGoodsRelatedVo.setGoodsList(goodsArr);

        return wxGoodsRelatedVo;
    }

    private boolean existGoodsNameOfUpdate(Goods goods) {
        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria criteria = goodsExample.createCriteria();
        //在此id之外的goods中查重name
        criteria.andIdNotEqualTo(goods.getId()).andNameEqualTo(goods.getName()).andDeletedEqualTo(false);
        List<Goods> goods1 = goodsMapper.selectByExample(goodsExample);
        if (goods1.size() > 0) {
            return true;
        }
        return false;
    }

    private boolean existGoodsSnOfUpdate(Goods goods) {
        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria criteria = goodsExample.createCriteria();
        //在此id之外的goods中查重goodsSn
        criteria.andIdNotEqualTo(goods.getId()).andGoodsSnEqualTo(goods.getGoodsSn()).andDeletedEqualTo(false);
        List<Goods> goods1 = goodsMapper.selectByExample(goodsExample);
        if (goods1.size() > 0) {
            return true;
        }
        return false;
    }
}
